package com.nms.service.impl;

import java.util.List;

import org.springframework.transaction.annotation.Transactional;

import com.nms.dao.UserDao;
import com.nms.model.User;
import com.nms.service.UserService;


//@Component("userService")
public class UserServiceImpl implements UserService {

	private UserDao userDao;

	@Transactional
	@Override
	public int register(User user) {
		if(userDao.add(user))
			return 1;
		return 0;
	}

	@Transactional
	@Override
	public boolean delete(User user) {
		return userDao.delete(user);
	}

	@Transactional
	@Override
	public boolean deleteByUsername(String username) {
		return userDao.deleteByUsername(username);
	}

	@Transactional
	@Override
	public User find(String username) {
		return userDao.findByUsername(username);
	}

	@Transactional
	@Override
	public boolean update(User user) {
		return userDao.update(user);
	}
	@Transactional
	@Override
	public User login(User user) {
		User u = userDao.findByUsername(user.getUsername());
		if(u == null || !(user.getPassword().equals(u.getPassword())))
			return null;
		return u;
	}

	@Transactional
	@Override
	public List<User> findAll() {
		return userDao.findAll();
	}

	///////////////////////////////////////////////////
	public UserDao getUserDao() {
		return userDao;
	}
	
	public void setUserDao(UserDao userDao) {
		this.userDao = userDao;
	}

	@Transactional
	@Override
	public boolean resetPw(String username) {
		return userDao.setUserPw(username, "888888");
	}
}
